﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using WRSManagementSystem.DomainModel.Entities;
using WRSManagementSystem.DomainModel.Repositories;

namespace WRSManagementSystem.Controllers
{
    public class StoreItemController : Controller
    {
        private readonly IStoreItemsRepository _storeItemrepo;
        private readonly ITypeRepository _typerepo;
        private readonly IUnitsRepository _unitsrepo;

        public StoreItemController(IStoreItemsRepository storeItemrepo, ITypeRepository typerepo, IUnitsRepository unitsrepo)
        {
            _storeItemrepo = storeItemrepo;
            _typerepo = typerepo;
            _unitsrepo = unitsrepo;
        }


        //
        // GET: /StoreItem/

        public ActionResult Index()
        {
            ViewData["Units"] = GetUnits();
            return View(_storeItemrepo.Find());
        }

        //
        // GET: /StoreItem/Details/5

        public ActionResult Details(int id)
        {
            return View(_storeItemrepo.FindOne(item => item.Id == id));
        }

        //
        // GET: /StoreItem/Create

        public ActionResult Create()
        {
            ViewData["Types"] = GetTypesSelectList();
            return View(new StoreItem());
        }

        private object GetUnit(StoreItem item)
        {
            return new SelectList(_unitsrepo.Find(u => u.Id == item.Type.Unit.Id).Select(u => u.ShortCut));
        }

        private object GetUnits()
        {
            return new SelectList(_unitsrepo.Find().Select(u => u.ShortCut));
        }


        private SelectList GetTypesSelectList()
        {
            return new SelectList(_typerepo.Find(), "Id", "Name");
        } 

        //
        // POST: /StoreItem/Create

        [HttpPost]
        public ActionResult Create(StoreItem model)
        {
            ViewData["Types"] = GetTypesSelectList();
            model.Type = _typerepo.FindOne(t => t.Id == model.Type.Id);

            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    _storeItemrepo.Add(model);
                    _storeItemrepo.Save();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(model);
                } 
            }
            return View(model);
        }

        //
        // GET: /StoreItem/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_storeItemrepo.FindOne(item => item.Id == id));
        }

        //
        // POST: /StoreItem/Edit/5

        [HttpPost]
        public ActionResult Edit(StoreItem model)
        {
            //ViewData["Types"] = GetTypesSelectList();
            //model.Type = _typerepo.FindOne(t => t.Id == model.Type.Id);

            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    _storeItemrepo.Update(model);
                    _storeItemrepo.Save();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(model);
                } 
            }
            return View(model);
        }
    }
}
